#include<iostream>
#include<algorithm>
#include<assert.h>
#include<string.h>
#include<deque>
#include <conio.h>
#include<graphics.h>
#include<time.h>
#include<chrono>
#include<cstring>
#include"button.h"
#include"Rand.h"
#include"zubi.h"
using namespace std;

int Time[5] = { 0,15,18,22,26}; // 业务预计处理时间
int comTime[5];     // 需排队时间
int comvipTime;
int averagrtime[5]; // 平均处理时间
int ktime; // 用于倒计时
int yewu[5];

deque<Node> comone, comtwo, comvip, comvipcom, comfour; // 1 号口，2号口，3号vip口中的vip的信息，3号vip口普通人的信息.4号口


void OutTime(int currentTime) 
{
	if (comone.size() && currentTime == comone.front().time)
	{
		averagrtime[1] += Time[comone.front().feature];
		//comTime[1] -= Time[comone.front().feature];
		
		comone.pop_front();
	}
	if (comtwo.size() && currentTime == comtwo.front().time)
	{
		averagrtime[2] += Time[comtwo.front().feature];
		//comTime[2] -= Time[comtwo.front().feature];
		comtwo.pop_front();
	}
	if (comvip.size() && currentTime == comvip.front().time)
	{
		averagrtime[3] += Time[comvip.front().feature];
		//comTime[3] -= Time[comvip.front().feature];
		comvip.pop_front();
	}
	else if (comvipcom.size() && currentTime == comvipcom.front().time)
	{
		averagrtime[3] += Time[comvipcom.front().feature];
		//comTime[3] -= Time[comvipcom.front().feature];
		yewu[3] = 0;
		comvipcom.pop_front();
	}
	if (comfour.size() && currentTime == comfour.front().time)
	{
		averagrtime[4] += Time[comfour.front().feature];
		//comTime[4] -= Time[comfour.front().feature];
		comfour.pop_front();
	}
	if (currentTime != ktime)
	{
		ktime = currentTime;
		if (comTime[1])comTime[1] --;
		if (comTime[2])comTime[2]--;
		if (comTime[3])comTime[3]--;
		if (comTime[4])comTime[4]--;
		if (comvipTime)comvipTime--;
		yewu[3] ++;
		bool st = true;                               // true 为当前vip ，false 为当前普通人
		if (comvip.empty() && comvipcom.size()) st = false;                                     // 没vip, 有普通人
		else if (comvip.size() && comvipcom.size()  && comvip.front().time > comvipcom.front().time) st = false;                      // 都有，不过普通人正在办理业务
		if (!st && comvipcom.size())
		{
			yewu[3] %= comvipcom.front().time;
		}
	}
}

void Out(string s,int vip,int kind) // 输出随机生成的数据
{
	cout << "姓名 ： " << s << "     " << "vip ： " << vip << "    " << "办理业务类型 ： " << kind <<  '\n';
}

void enqueue(int kind) // 随机生成数据，并且根据时间改变
{
	string ipt = testRamdFunc(); // 随机名字
	int vip = randvip();         // 随机是否是vip
	Node ren;
	ren.name = ipt;
	ren.feature = kind;
	Out(ipt,vip,kind);
	auto now = chrono::system_clock::now(); // 获取当前时间
	time_t currentTime = chrono::system_clock::to_time_t(now); // 获取当前时间的值
	if (comone.empty())
	{
		ren.time = currentTime + comTime[1] + Time[kind];
		comTime[1] += Time[kind];
		comone.push_back(ren);
	}
	else if (comtwo.empty())
	{
		ren.time = currentTime + comTime[2] + Time[kind];
		comTime[2] += Time[kind];
		comtwo.push_back(ren);
	}
	else if (comvip.empty() && vip && comvipcom.empty())
	{
		ren.time = currentTime + Time[kind];
		comvipTime += Time[kind];
		comTime[3] += Time[kind];
		comvip.push_back(ren);
	}
	else if (comvipcom.empty() && !vip && comvip.empty())
	{
		ren.time = currentTime + Time[kind];
		comTime[3] += Time[kind];
		comvipcom.push_back(ren);
	}
	else if (comfour.empty())
	{
		ren.time = currentTime + comTime[4] + Time[kind];
		comTime[4]+= Time[kind];
		comfour.push_back(ren);
	}
	else
	{
		int p = 1; 
		int Times = comTime[1];
		if (vip)
		{
			for (int i = 2; i <= 4; i++) // 找时间最短的  
			{
				if (i == 3 ) // vip 队伍
				{
					if(comvipTime < Times)
					{
						Times = comvipTime;
						p = 3;
					}
					continue;
				}
				if(comTime[i] < Times)
				{
					p = i;
					Times = comTime[i];
				}
			}
			if (p == 1)
			{
				ren.time = currentTime + comTime[1] + Time[kind];
				comone.push_back(ren);
				comTime[1] += Time[kind];
			}
			else if (p == 2)
			{
				ren.time = currentTime + comTime[2] + Time[kind];
				comtwo.push_back(ren);
				comTime[2] += Time[kind];
			}
			else if (p == 3)
			{
				bool st = true;                               // true 为当前vip ，false 为当前普通人
				if (comvip.empty() && comvipcom.size()) st = false;                                     // 没vip, 有普通人
				else if (comvip.size() && comvipcom.size() && comvip.front().time > comvipcom.front().time) st = false;                      // 都有，不过普通人正在办理业务

				if (st) // 直接赋值就行
				{
					ren.time = currentTime + comvipTime + Time[kind];
					comvip.push_back(ren);
					comTime[3] += Time[kind];

					comvipTime += Time[kind];
					deque<Node> Temporary;
					Times = comTime[1];
					while (comvipcom.size())
					{
						Node t = comvipcom.back();
						comvipcom.pop_back();
						p = 1;
						for (int i = 2; i <= 4; i++) // 找时间最短的  
						{
							if (i != 3 && comTime[i] + Time[t.feature] < Times)
							{
								p = i;
								Times = comTime[i];
							}
							else if (i == 3 && comTime[i] < Times)
							{
								p = i;
								Times = comTime[i];
							}
						}
						if (p == 3)
						{
							t.time = t.time + Time[kind]; // 前面有vip 所以后面的普通人增加时间
							Temporary.push_front(t);
							//cout << 666 << endl;
						}
						else
						{
							comTime[3] -= Time[t.feature];
							if (p == 1)
							{
								t.time = comone.back().time + Time[t.feature];
								comone.push_back(t); comTime[1] += Time[t.feature];
							}
							else if (p == 2)
							{
								t.time = comtwo.back().time + Time[t.feature];
								comtwo.push_back(t); comTime[2] += Time[t.feature];
							}
							else if (p == 4)
							{
								t.time = comfour.back().time + Time[t.feature];
								comfour.push_back(t); comTime[4] += Time[t.feature];
							}
							//cout << 7788 << endl;
						}
					}
					comvipcom = Temporary; // 重新赋值
				}
				else                            // 普通人正在办理业务
				{
					if (comvip.empty()) ren.time = comvipcom.front().time + Time[kind] ; // 排在当前办理业务普通人后面
					else ren.time = comvip.back().time + Time[kind];
					comvip.push_back(ren);
					comTime[3] += Time[kind];
					comvipTime += Time[kind];
					deque<Node> Temporary;
					Times = comTime[1];
					while (comvipcom.size() > 1)
					{
						p = 1;
						Node t = comvipcom.back();
						comvipcom.pop_back();
						for (int i = 2; i <= 4; i++) // 找时间最短的  
						{
							if (i != 3 && comTime[i] + Time[t.feature] < Times)
							{
								p = i;
								Times = comTime[i];
							}
							else if (i == 3 && comTime[t.feature] < Times)
							{
								p = i;
								Times = comTime[i];
							}
						}
						if (p == 3)
						{
							t.time = t.time + Time[kind]; // 前面有vip 所以后面的普通人增加时间
							Temporary.push_front(t);
							//cout << 666 << endl;
						}
						else
						{
							comTime[3] -= Time[t.feature];
							if (p == 1 && comone.size())
							{
								t.time = comone.back().time + Time[t.feature];
								comone.push_back(t), comTime[1] += Time[t.feature];
							}
							else if (p == 2 && comtwo.size())
							{
								t.time = comtwo.back().time + Time[t.feature];
								comtwo.push_back(t), comTime[2] += Time[t.feature];
							}
							else if (p == 4 && comfour.size())
							{
								t.time = comfour.back().time + Time[t.feature];
								comfour.push_back(t), comTime[4] += Time[t.feature];
							}
							//cout << 77 << endl;
						}
					}
					Temporary.push_front(comvipcom.front());
					comvipcom = Temporary; // 重新赋值
				}
			}
			else if (p == 4)
			{
				ren.time = currentTime + comTime[4] + Time[kind];
				comfour.push_back(ren);
				comTime[4] += Time[kind];
			}
		}
		else
		{
			for (int i = 2; i <= 4; i++) // 找时间最短的  
			{
				if (comTime[i] < Times)
				{
					p = i;
					Times = comTime[i];
				}
			}
			if (p == 1)
			{
				ren.time = currentTime + comTime[1] + Time[kind];
				comone.push_back(ren);
				comTime[1] += Time[kind];
			}
			else if (p == 2)
			{
				ren.time = currentTime + comTime[2] + Time[kind];
				comtwo.push_back(ren);
				comTime[2] += Time[kind];
			}
			else if (p == 3)
			{
				ren.time = currentTime + comTime[3] + Time[kind];
				comvipcom.push_back(ren);
				comTime[3] += Time[kind];
			}
			else if (p == 4)
			{
				ren.time = currentTime + comTime[4] + Time[kind];
				comfour.push_back(ren);
				comTime[4] += Time[kind];
			}
		}
	}
	cout << comTime[1] << " " << comTime[2] << " " << comTime[3] << " " << comTime[4] << endl;
}

void display()
{
	Button* windowone = createButton(25, 50, 200, 80, "普通窗口(一)", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* windowtwo = createButton(275, 50, 200, 80, "普通窗口(二)", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* windowthire = createButton(525, 50, 200, 80, "vip窗口(三)", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* windowfour = createButton(775, 50, 200, 80, "普通窗口(四)", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* Return = createButton(775, 0, 100, 40, "返回", RGB(236, 244, 255), RGB(204, 213, 240));
	setbkcolor(RGB(56, 99, 176));
	cleardevice();
	deque<Node> q1 = comone, q2 = comtwo, vip3 = comvip, vip3com = comvipcom, q4 = comfour;
	ExMessage m;
	
	drawButton(windowone);
	drawButton(windowtwo);
	drawButton(windowthire);
	drawButton(windowfour);
	drawButton(Return);
	for (int i = 25; i <= 800; i += 250)
	{
		if(i == 25)
		{
			for (int j = 150; j <= 700; j += 100)
			{
				if (q1.size())
				{
					string s = q1.front().name;
					q1.pop_front();
					Button* pailie;
					if(j == 150)pailie = createButton(i, j, 200, 80, s, RGB(41, 209, 27), RGB(41, 209, 27));
					else pailie = createButton(i, j, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
					drawButton(pailie);
				}
			}
			string s; int d = comTime[1];
			if (d == 0) s += '0';
			else 
			while (d)
			{
				s += d % 10 + '0';
				d /= 10;
			}
			reverse(s.begin(), s.end());
			s += '\0';
			Button* avaregtime = createButton(i, 720, 200, 80,s, RGB(236, 244, 255), RGB(204, 213, 240));
			drawButton(avaregtime);
		}
		else if (i == 275)
		{
			for (int j = 150; j <= 700; j += 100)
			{
				if (q2.size())
				{
					string s = q2.front().name;
					q2.pop_front();
					Button* pailie;
					if(j == 150) pailie = createButton(i, j, 200, 80, s, RGB(41, 209, 27), RGB(41, 209, 27));
					else pailie = createButton(i, j, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
					drawButton(pailie);
				}
			}
			string s; int d = comTime[2];
			if (d == 0) s += '0';
			else
			while (d)
			{
				s += d % 10 + '0';
				d /= 10;
			}
			reverse(s.begin(), s.end());
			s += '\0';
			Button* avaregtime = createButton(i, 720, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
			drawButton(avaregtime);
		}
		
		else if (i == 525)
		{
			for (int j = 150; j <= 700; j += 100)
			{
				if (j == 150 &&  vip3.size() &&  vip3com.size() && vip3com.front().time < vip3.front().time)
				{
					string s = vip3com.front().name;
					vip3com.pop_front(); // 41, 209, 27
					Button* pailie;
					pailie = createButton(i, j, 200, 80, s, RGB(41, 209, 27), RGB(41, 209, 27));
					drawButton(pailie);
				}
				else if (vip3.size())
				{
					string s = vip3.front().name;
					vip3.pop_front(); // 41, 209, 27
					Button* pailie ;
					if(j == 150)pailie = createButton(i, j, 200, 80, s, RGB(41, 209, 27), RGB(41, 209, 27));
					else pailie = createButton(i, j, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
					drawButton(pailie);
				}
				else if (vip3com.size())
				{
					string s = vip3com.front().name;
					vip3com.pop_front();
					Button* pailie;
					if (j == 150)pailie = createButton(i, j, 200, 80, s, RGB(41, 209, 27), RGB(41, 209, 27));
					else pailie = createButton(i, j, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
					drawButton(pailie);
				}
			}
			string s1;
			int we = comvipTime;
			string s; int d = comTime[3];
			if (we == 0) s1 += '0';
			else 
			while (we)
			{
				s1 += we % 10 + '0';
				we /= 10;
			}
			reverse(s1.begin(),s1.end());
			s1 += '\0';
			if (d == 0) s += '0';
			else 
			while (d)
			{
				s += d % 10 + '0';
				d /= 10;
			}
			reverse(s.begin(), s.end());
			s += '\0';
			Button* avaregtime1 = createButton(i, 720, 200, 35, s1, RGB(236, 244, 255), RGB(204, 213, 240));
			Button* avaregtime2 = createButton(i, 755, 200, 35, s, RGB(236, 244, 255), RGB(204, 213, 240));
			drawButton(avaregtime1);
			drawButton(avaregtime2);
		}
		else if (i == 775)
		{
			for (int j = 150; j <= 700; j += 100)
			{

				if (q4.size())
				{
					string s = q4.front().name;
					q4.pop_front();
					Button* pailie;
					if(j == 150) pailie = createButton(i, j, 200, 80, s, RGB(41, 209, 27), RGB(41, 209, 27));
					else pailie = createButton(i, j, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
					drawButton(pailie);
				}
			}
			string s; int d = comTime[4];
			if (d == 0) s += '0';
			else 
			while (d)
			{
				s += d % 10 + '0';
				d /= 10;
			}
			reverse(s.begin(), s.end());
			s += '\0';
			Button* avaregtime = createButton(i, 720, 200, 80, s, RGB(236, 244, 255), RGB(204, 213, 240));
			drawButton(avaregtime);
		}
	}
	BeginBatchDraw();
	while (1)
	{
		auto now = chrono::system_clock::now(); // 获取当前时间
		time_t currentTime = chrono::system_clock::to_time_t(now); // 获取当前时间的值
		OutTime(currentTime);
		drawButton(windowone);
		drawButton(windowtwo);
		drawButton(windowthire);
		drawButton(windowfour);
		drawButton(Return);
		peekmessage(&m, EM_MOUSE); // 鼠标位置
		if (isClickButton(Return, m))
		{
			return;
		}
		FlushBatchDraw();
	}
	EndBatchDraw();
}

void menu()
{
	initgraph(1000, 900);
	setbkcolor(RGB(106, 108, 255));
	cleardevice();
	Button* deposit = createButton(300, 50, 450, 80, "存款业务", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* debt = createButton(300, 150, 450, 80, "贷款业务", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* investment = createButton(300, 250, 450, 80, "债券投资业务", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* middle = createButton(300, 350, 450, 80, "中间业务", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* Display = createButton(300, 450, 450, 80, "实时显示", RGB(236, 244, 255), RGB(204, 213, 240));
	Button* fanying = createButton(300, 550, 450, 80, "反应", RGB(236, 244, 255), RGB(204, 213, 240));
	ExMessage m;
	BeginBatchDraw();
	bool we1 = true;
	while (1)
	{
		setbkcolor(RGB(56, 99, 176));
		drawButton(deposit);
		drawButton(debt);
		drawButton(investment);
		drawButton(middle);
		drawButton(Display);
		drawButton(fanying);
		peekmessage(&m, EM_MOUSE); // 鼠标位置

		auto now = chrono::system_clock::now(); // 获取当前时间
		time_t currentTime = chrono::system_clock::to_time_t(now); // 获取当前时间的值
		OutTime(currentTime);
		if (we1 && isClickButton(deposit, m))            // 存钱
		{
			enqueue(1);	
			cleardevice();
			we1 = false;
		}
		if (we1 && isClickButton(debt, m))               // 贷款
		{
			enqueue(2);
			cleardevice();
			we1 = false;
		}
		if (we1 && isClickButton(investment, m))         // 投资
		{
			enqueue(3); 
			cleardevice();// 循环
			we1 = false;
		}
		if (we1 && isClickButton(middle, m))             // 中间业务
		{
			enqueue(4);   // 循环
			cleardevice();
			we1 = false;
		}
		
		if (we1 && isClickButton(Display, m))
		{
			display();   // 显示出所有窗口的数据
			cleardevice();
			we1 = false;
		}
		if (isClickButton(fanying,m))
		{
			we1 = true;
		}
		FlushBatchDraw();
	}
	EndBatchDraw();
	closegraph();
	return;
}

int main()
{
	menu();
}
